Repository navigation
RHINENG-33916: keep database credentials out of the connection string - #2399
Conversation
Set user, password, and database on the pgx config so special characters in those values cannot change the host or TLS settings.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #2399 +/- ##
==========================================
+ Coverage 66.82% 66.86% +0.04%
==========================================
Files 158 158
Lines 9081 9090 +9
==========================================
+ Hits 6068 6078 +10
+ Misses 3011 3010 -1
Partials 2 2
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Reviewer's GuideAPIIntroduce the shared pgx configuration and database-opening helpers that keep credentials out of PostgreSQL connection strings.
Uses of the APISwitch migration connections and administrative SQL to the shared pgx configuration, safely quote identifiers and passwords, and close the database if migration setup fails.
ConfigurationRemove per-service database password environment variables so services use the centralized credentials.
Review the changed environment and application configuration files whose diff contents are not shown.
TestsAdd a test proving credentials and statement timeout stay out of the connection string while TLS configuration is preserved.
Lock files and vendored codePromote pgx to a direct module dependency for the new structured connection implementation.
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Hey - I've found 7 issues
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path="base/database/setup.go" line_range="180" />
<code_context>
+ dbConfig.Host, dbConfig.Port, dbConfig.SSLMode)
if dbConfig.SSLRootCert != "" {
- dbsource = fmt.Sprintf("%s sslrootcert=%s", dbsource, dbConfig.SSLRootCert)
+ dsn += " sslrootcert=" + dbConfig.SSLRootCert
+ }
+ cfg, err := pgx.ParseConfigWithOptions(dsn, pgx.ParseConfigOptions{
</code_context>
<issue_to_address>
**TLS mode can be overridden**
When `SSLRootCert` contains whitespace followed by a keyword DSN option such as `sslmode=disable`, the root-certificate path is appended unescaped to the keyword DSN, so a value containing a token such as `sslmode=disable` is parsed as another allowed setting and overrides the configured TLS mode. The connection then stops verifying TLS.
Escape or quote DSN values before parsing, or avoid interpolating the certificate path into the connection string.
</issue_to_address>
### Comment 2
<location path="database_admin/update.go" line_range="288" />
<code_context>
- execOrPanic(db, "ALTER USER manager WITH PASSWORD '"+utils.GetenvOrFail("MANAGER_PASSWORD")+"'")
- execOrPanic(db, "ALTER USER vmaas_sync WITH PASSWORD '"+utils.GetenvOrFail("VMAAS_SYNC_PASSWORD")+"'")
+ for _, user := range lockUsers {
+ setUserPassword(db, user, utils.CoreCfg.DBPassword)
+ }
}
</code_context>
<issue_to_address>
**Shared password erases role isolation**
When an application service is compromised and its database credential is recovered, `setUserPassword` assigns the same `DBPassword` to every login in `lockUsers`, so a compromised service that knows its own database password can authenticate as the other application roles and obtain their database privileges.
Keep distinct passwords for each database role, or use a credential design that prevents one service from authenticating as the other roles.
</issue_to_address>
### Comment 3
<location path="database_admin/update.go" line_range="288" />
<code_context>
- execOrPanic(db, "ALTER USER manager WITH PASSWORD '"+utils.GetenvOrFail("MANAGER_PASSWORD")+"'")
- execOrPanic(db, "ALTER USER vmaas_sync WITH PASSWORD '"+utils.GetenvOrFail("VMAAS_SYNC_PASSWORD")+"'")
+ for _, user := range lockUsers {
+ setUserPassword(db, user, utils.CoreCfg.DBPassword)
+ }
}
</code_context>
<issue_to_address>
**Application pods lose database access**
When a deployment rotates per-service passwords while old pods still use those credentials, `setUserPassword` assigns the shared `CoreCfg.DBPassword` to every application role, so PostgreSQL rejects reconnects from pods using their previous per-service passwords; components expecting distinct credentials also fail authentication, and application database access is interrupted during the rollout.
Keep each application role's password aligned with the credentials used by its pods throughout the rollout.
Also at `deploy/clowdapp.yaml:96-97`.
</issue_to_address>
### Comment 4
<location path="database_admin/update.go" line_range="288" />
<code_context>
- execOrPanic(db, "ALTER USER manager WITH PASSWORD '"+utils.GetenvOrFail("MANAGER_PASSWORD")+"'")
- execOrPanic(db, "ALTER USER vmaas_sync WITH PASSWORD '"+utils.GetenvOrFail("VMAAS_SYNC_PASSWORD")+"'")
+ for _, user := range lockUsers {
+ setUserPassword(db, user, utils.CoreCfg.DBPassword)
+ }
}
</code_context>
<issue_to_address>
**New pods fail database authentication**
When `update_users` is unset and existing database role passwords differ from Clowder's password, `UpdateDB` skips `setUserPassword`, leaving the roles' old passwords in place while new pods use Clowder's password; PostgreSQL rejects their connections.
Ensure database roles are synchronized with the credentials supplied to new pods even when `update_users` is unset.
Also at `deploy/clowdapp.yaml:97`.
</issue_to_address>
### Comment 5
<location path="database_admin/update.go" line_range="288" />
<code_context>
- execOrPanic(db, "ALTER USER manager WITH PASSWORD '"+utils.GetenvOrFail("MANAGER_PASSWORD")+"'")
- execOrPanic(db, "ALTER USER vmaas_sync WITH PASSWORD '"+utils.GetenvOrFail("VMAAS_SYNC_PASSWORD")+"'")
+ for _, user := range lockUsers {
+ setUserPassword(db, user, utils.CoreCfg.DBPassword)
+ }
}
</code_context>
<issue_to_address>
**Some roles keep stale passwords**
When a password update fails after one or more roles have been updated, `setUserPassword` commits each role's change independently, so a later error leaves the roles with mixed passwords and the migration fails; services using the shared credential cannot connect to roles that were not updated.
Update all application-role passwords atomically so a failure leaves every role unchanged.
</issue_to_address>
### Comment 6
<location path="database_admin/update.go" line_range="222-224" />
<code_context>
+ if err != nil {
+ panic(err)
+ }
+ cfg.RuntimeParams["tcp_keepalives_idle"] = "60"
+ cfg.RuntimeParams["tcp_keepalives_interval"] = "30"
+ cfg.RuntimeParams["tcp_keepalives_count"] = "5"
+ cfg.OnNotice = func(_ *pgconn.PgConn, notice *pgconn.Notice) {
+ utils.LogInfo("Notice: " + notice.Severity + ": " + notice.Message)
</code_context>
<issue_to_address>
**Admin connections lose client keepalives**
When an admin connection is idle on a network that drops or silently blackholes database connections, `cfg.RuntimeParams` sends `tcp_keepalives_*` as PostgreSQL startup parameters, configuring server-side settings instead of the admin client's TCP socket; the connection loses the configured client keepalive behavior and can be dropped during idle periods or take longer to detect a blackhole.
Configure the keepalive settings on the admin client's TCP socket instead of passing them through `RuntimeParams`.
</issue_to_address>
### Comment 7
<location path="database_admin/migrate.go" line_range="28" />
<code_context>
-func NewConn(databaseURL string) (database.Driver, *sql.DB, error) {
- baseConn, err := pq.NewConnector(databaseURL)
- if err != nil {
- return nil, nil, err
- }
-
</code_context>
<issue_to_address>
**Connection error is obscured**
When `db.Exec` or `postgres.WithInstance` fails in `NewConn`, `NewConn` returns a nil `*sql.DB` with the error, so `dbConn` calls `Close` on nil and panics with a nil-pointer error instead of reporting the connection failure; the opened pool is also not closed.
Have `NewConn` return the original `db` on errors, or only close it in `dbConn` when it is non-nil.
Also at `database_admin/update.go:233`.
</issue_to_address>Sourcery assessment
Needs a human reviewer. 7 findings to address first, and on database-admin startup, this change assigns the same configured password to every application database role, replacing their separate passwords. Those credentials persist in PostgreSQL after a revert, so restoring the old role-specific access requires explicitly rotating them.
Blocking findings: base/database/setup.go:180, database_admin/update.go:288, database_admin/update.go:288, database_admin/update.go:288, database_admin/update.go:288, and 2 more
There was a problem hiding this comment.
Did my best to review with the help of codex, lgtm! 😄 💯 Only question/comment I had was related to the possible nil-panic after the NewConn function call - if you think it should be changed just ping me when you push the changes and I'll give another quick ack! Thanks
johnelliott626
left a comment
There was a problem hiding this comment.
Thanks for making that change! ACK 💯 ✅ 😄
Secure Coding Practices Checklist GitHub Link
Secure Coding Checklist
Summary by Sourcery
Keep PostgreSQL credentials out of connection strings by adopting structured connection configuration across application, migration, and deployment database access.
Bug Fixes:
Enhancements:
Build:
Deployment:
Tests:
Chores: